﻿using CBS.APP;
using System.Reflection;
using System.ServiceProcess;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CBS.Desktop
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private int SelectMenuIndex { get; set; } = 0;
        private List<WorkPlaceItem> WorkPlaces { get; set; } = new List<WorkPlaceItem>()
        {
            { new WorkPlaceItem(){ WorkPlaceName="SemesterList", Title="学期管理", isActive=true }},
            { new WorkPlaceItem(){ WorkPlaceName="TeacherManage", Title="教师管理", isActive=false }},
            { new WorkPlaceItem(){ WorkPlaceName="GradeManage", Title="年级管理", isActive=false }},
            { new WorkPlaceItem(){ WorkPlaceName="ClassManage", Title="班级管理", isActive=false }},
            { new WorkPlaceItem(){ WorkPlaceName="CourseManage", Title="课程管理", isActive=false }},
            { new WorkPlaceItem(){ WorkPlaceName="CoursePlanManage", Title="课程计划", isActive=false }},
            { new WorkPlaceItem(){ WorkPlaceName="LessonDetail", Title="开课管理", isActive=false }},
            { new WorkPlaceItem(){ WorkPlaceName="StudentManage", Title="学生管理", isActive=false }},
            { new WorkPlaceItem(){ WorkPlaceName="StudentSignUp", Title="学生报到", isActive=false }},
            { new WorkPlaceItem(){ WorkPlaceName="StudentSignDetail", Title="学生签到管理", isActive=false }},
            { new WorkPlaceItem(){ WorkPlaceName="StudentBonus", Title="分红管理", isActive=false }},
            { new WorkPlaceItem(){ WorkPlaceName="StudentPayment", Title="学生缴费记录", isActive=false }},
            { new WorkPlaceItem(){ WorkPlaceName="StudentCost", Title="学生消费记录", isActive=false }},
            { new WorkPlaceItem(){ WorkPlaceName="StudentSemesterAmount", Title="学生账户结算", isActive=false }},
            { new WorkPlaceItem(){ WorkPlaceName="RemunerationStatement", Title="教师转账管理", isActive=false }},
            { new WorkPlaceItem(){ WorkPlaceName="TeacherSemesterAmount", Title="教师账户结算", isActive=false }},
            { new WorkPlaceItem(){ WorkPlaceName="SystemStatement", Title="报表查询", isActive=false }},
        };
        public MainWindow()
        {
            InitializeComponent();

            //btn_CourseManage.IsEnabled = false;
            tc_Menu.Items.Clear();
            foreach (var item in WorkPlaces)
            {
                HandyControl.Controls.TabItem itemTab = new HandyControl.Controls.TabItem();
                itemTab.Header = item.Title;
                itemTab.Height = 45;
                itemTab.Width = 150;
                itemTab.HorizontalAlignment = HorizontalAlignment.Right;

                switch (item.WorkPlaceName)
                {
                    case "StudentSemesterAmount":
                        itemTab.ToolTip = "学生账户结算(分学期)";
                        break;
                    case "TeacherSemesterAmount":
                        itemTab.ToolTip = "教师账户结算(分学期)";
                        break;
                    default:
                        break;
                }
                // 设置ToolTipService.InitialShowDelay
                ToolTipService.SetInitialShowDelay(itemTab, 0);
                tc_Menu.Items.Add(itemTab);

            }

            

        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            base.OnClosing(e);

            // 检查并关闭 MySQL 服务
            //CheckAndStopMySQLService();
        }

        private void CheckAndStopMySQLService()
        {
            const string serviceName = "MySQL"; // 根据实际的服务名称修改

            using (ServiceController service = new ServiceController(serviceName))
            {
                try
                {
                    // 检查服务状态
                    if (service.Status == ServiceControllerStatus.Running)
                    {
                        // 如果服务正在运行，则停止服务
                        service.Stop();
                        service.WaitForStatus(ServiceControllerStatus.Stopped); // 等待服务停止
                    }

                    MessageBox.Show("MySQL 服务已停止，继续关闭应用程序。");
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"无法检查或停止 MySQL 服务: {ex.Message}");
                    // 这里可以选择是否取消关闭操作
                    // e.Cancel = true; // Uncomment this line if you want to prevent closing
                }
            }
        }


        public void FlushWorkPlace(int activeIndex)
        {

            dp_WorkPlace.Children.Clear();
            WorkPlaces[SelectMenuIndex].isActive = false;


            SelectMenuIndex = activeIndex;
            WorkPlaces[SelectMenuIndex].isActive = true;

            Assembly assembly = Assembly.GetExecutingAssembly(); // 获取当前程序集 
            dynamic obj = assembly.CreateInstance($"CBS.Desktop.ViewPartial.{WorkPlaces[SelectMenuIndex].WorkPlaceName}"); // 创建类的实例，返回为 object 类型，需要强制类型转换
            dp_WorkPlace.Children.Add(obj);
        }

        public class WorkPlaceItem
        {
            public required string WorkPlaceName { get; set; }
            public required string Title { get; set; }
            public bool isActive { get; set; }

            //public required string AssemblyName { get; set; }
        }

        private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            FlushWorkPlace(tc_Menu.SelectedIndex);
        }
    }
}